package org.example.myerp.controller;

import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.example.myerp.pojo.PageMyself;
import org.example.myerp.pojo.User;
import org.example.myerp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("/system/user")
public class UserController {

	@Autowired
    UserService userService;

	@RequestMapping("/getUsers")
	public List<User> getUsers() {
		return userService.getUsers();
	}

	@RequestMapping("/login")
	public User login(String uname, String upwd, HttpSession session, HttpServletResponse response ) {
		User user = new User();
		user = userService.login(uname, upwd);
		if (user != null) {
			if (user.getStatics().equals("开启")) {
				UUID randomUUID = UUID.randomUUID();
				System.err.println(randomUUID);
				user.setToken(randomUUID.toString());
				session.setAttribute("user", user);
			}
		}
        //response.setHeader("Access-Control-Allow-Origin", "http://localhost:8081");
		return user;
	}

	@RequestMapping("/findByPager")
	public PageMyself<User> findByPager(Integer pageNum, Integer pageSize) {
		return userService.findByPager(pageNum, pageSize);
	}

	@RequestMapping("/addUser")
	public void addUser(String uName, String uPwd, String statics) {
		User user = new User();
		Random r = new Random();
		int nextInt = r.nextInt(1000000000);
		user.setResperson(String.valueOf(nextInt));
		user.setuName(uName);
		user.setuPwd(uPwd);
		user.setStatics(statics);
		userService.addUser(user);
	}

	@RequestMapping("/updateUser")
	public void updateUser(String uName, String uPwd, String statics, Integer uid) {
		User user = new User();
		user.setuName(uName);
		user.setuPwd(uPwd);
		user.setStatics(statics);
		user.setUid(uid);
		userService.updateUser(user);
	}

	// ����ɾ��
	@RequestMapping("/batchDelUser")
	public void batchDelUser(Integer[] uids) {
		userService.batchDelUser(uids);
		int i = 0;
	}

	@RequestMapping("/updateStatics")
	public void updateStatics(Integer uid, String statics) {
		userService.updateStatics(uid, statics);
	}

	@RequestMapping("/setUserRole")
	public void setUserRole(Integer[] rid, Integer uid) {
		userService.setUserRole(rid, uid);
	}

}
